//
//  SureIndentViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/15.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SureIndentViewController.h"
#import "PayViewController.h"
#import "MyForSaleCardTableViewCell.h"
#import "SureIndentTableViewCell.h"
#import "ScroButton.h"
#import "ShopViewController.h"
#import "ChoseAddressViewController.h"
static NSString * cellIndentifer = @"SureIndentTableViewCell";
static NSString * CellIndentifer = @"MyForSaleCardTableViewCell";
@interface SureIndentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UILabel *addressAddress;
@property (strong, nonatomic) IBOutlet UILabel *addressName;
@property (strong, nonatomic) IBOutlet UIView *addressView;
@property (strong, nonatomic) IBOutlet UIView *otherChoseView;
@property (strong, nonatomic) IBOutlet UIView *codePayView;
@property (strong, nonatomic) IBOutlet UIButton *maskView;
@property (strong, nonatomic) IBOutlet UIView *ifNeedPaperView;//是否需要发票
@property (strong, nonatomic) IBOutlet UITableView *forSaleTableView;//优惠劵tableview
@property (strong, nonatomic) IBOutlet UIView *forSaleView;//优惠劵view
@property(nonatomic,strong)NSMutableArray * forSalePaperArray;


@property (strong, nonatomic) IBOutlet UITextField *othersField;//单位
@property (strong, nonatomic) IBOutlet UILabel *codeUseRule;

@property (strong, nonatomic) IBOutlet UILabel *sumCode;
@property (strong, nonatomic) IBOutlet UILabel *codeRule;
@property (strong, nonatomic) IBOutlet UILabel *cutDownMoney;
@property (strong, nonatomic) IBOutlet UITextField *cutDownCode;

@property(nonatomic,strong)NSDictionary * addressDic;

@property (strong, nonatomic) IBOutlet UILabel *countLabel;


@property(nonatomic,strong)NSString * forSaleIds;//优惠券id
@property (strong, nonatomic) IBOutlet UILabel *ifUseForSalePaper;
@property (strong, nonatomic) IBOutlet UILabel *forSalePaperNumber;

@property(nonatomic,assign)NSInteger ifNeedPaperIndex;//是否需要发票50 51 52
@property (weak, nonatomic) IBOutlet UILabel *ifNeedPaperLabel;//是否需要发票label
@property(nonatomic,assign)NSInteger ifCodePayIndex;//是否抵扣积分53 54 55
@property(nonatomic,strong)ScroButton * scroButton;

@end

@implementation SureIndentViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    _ifNeedPaperIndex = 50;
    _ifCodePayIndex = 53;
    _forSalePaperArray = [NSMutableArray array];
    [self initViewLayOut];
    [self ifHaveForSalePaper];
    [self getPersonalCode];
    [self getDefaultAddress];

    

    // Do any additional setup after loading the view.
}

- (void)initViewLayOut{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.addressView];
    [self adaptiveViewLayout:self.otherChoseView];
    [self adaptiveViewLayout:self.codePayView];
    [self adaptiveViewLayout:_ifNeedPaperView];
    [self adaptiveViewLayout:self.forSaleView];
    
    NSInteger count= 0;
    for (NSDictionary * dic in _goodsInfoArray)  {
        NSArray * array = dic[@"goodsList"];
        count +=array.count;
    }
    if (count==0) {
        count = 1;
    }
    _tableView.frame = flexibleFrame(CGRectMake(0, 75, 320, _goodsInfoArray.count*38+count*102), YES);
    _otherChoseView.frame = flexibleFrame(CGRectMake(0, 75+CGRectGetHeight(_tableView.frame), 320, 248), YES);
    self.scroView.contentSize = flexibleSize(CGSizeMake(0,75+_goodsInfoArray.count*38+248+count*102), NO);

   
   [self.tableView registerNib:[UINib nibWithNibName:cellIndentifer bundle:nil] forCellReuseIdentifier:cellIndentifer];
    _tableView.scrollEnabled = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.forSaleTableView registerNib:[UINib nibWithNibName:CellIndentifer bundle:nil] forCellReuseIdentifier:CellIndentifer];
    self.forSaleTableView.dataSource = self;
    self.forSaleTableView.delegate = self;
    
    NSString * price = [NSString stringWithFormat:@"￥%.2f",[_countMoney floatValue]];
    NSString * number = [NSString stringWithFormat:@"%@",_number];
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"共%@件，总计：%@",number,price]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.859 green:0.161 blue:0.161 alpha:1.000] range:NSMakeRange(str.length-price.length, price.length)];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24*VerticalTexeRatio()] range:NSMakeRange(str.length-price.length, price.length)];
    _countLabel.attributedText = str;
    
    _scroButton = [[ScroButton alloc]initWithFrame:flexibleFrame(CGRectMake(263, 143, 49, 23), YES) title:@"" selectedTitle:@"" selectedColor:[UIColor colorWithRed:0.988 green:0.580 blue:0.055 alpha:1.000] cancelColor:[UIColor colorWithWhite:0.620 alpha:1.000] selected:NO oval:(BOOL)NO complect:^(NSInteger index) {

            if (index == 1) {
                _maskView.hidden = NO;
                _codePayView.hidden = NO;
            }
        
    }];
    [_otherChoseView addSubview:_scroButton];
}



- (IBAction)back:(id)sender {
     [self.navigationController  popViewControllerAnimated:YES];
}

//查询默认地址
- (void)getDefaultAddress{
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_GetDefaultAddress withHttpType:@"POST" withTag:SX_SX_GetDefaultAddress];
    
}



#pragma mark===== 增加地址
- (IBAction)addAddress:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    ChoseAddressViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ChoseAddress"];
    page.addressBlock = ^(NSDictionary * dic){
        _addressView.hidden = NO;
        _addressName.text = [NSString stringWithFormat:@"%@  %@",dic[@"msadReceiverName"],dic[@"msadMobileNo"]] ;
        _addressAddress.text = dic[@"msadAddr"];
        _addressDic = dic;
    
    };
    [self.navigationController   pushViewController:page animated:YES ];
}

#pragma mark====结算
- (IBAction)payHandel:(id)sender {
    if (!_addressDic) {
         [[CWHUDTips sharedInstance]  showTips:@"请选择地址"];
    }else{
        if ([self.payType isEqualToString:@"2"]) {//购物车购买
            NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
            NSString * storeIds= @"";
            NSString * GoodsInfos = @"";
            for (NSDictionary * dic in _goodsInfoArray) {
                NSString * stroeId = @"";
                NSString * goodsInfos = @"";
                NSArray * array = dic[@"goodsList"];
                for (NSDictionary * dict in array){
                    NSString * goodsInfo = @"";
                    if (dict == [array firstObject]) {
                        goodsInfo = [NSString stringWithFormat:@"%@:%@:%@",dict[@"mgooId"],dict[@"goodsCartNum"],dict[@"mgspId"]];
                    }else{
                        goodsInfo = [NSString stringWithFormat:@",%@:%@:%@",dict[@"mgooId"],dict[@"goodsCartNum"],dict[@"mgspId"]];
                    }
                    goodsInfos = [goodsInfos stringByAppendingString:goodsInfo];
                }
                if (dic == [_goodsInfoArray firstObject]) {
                    stroeId = [NSString stringWithFormat:@"%@",dic[@"mallStoreId"]];
                    
                }else{
                    stroeId = [NSString stringWithFormat:@",%@",dic[@"mallStoreId"]];
                    goodsInfos = [NSString stringWithFormat:@";%@",goodsInfos];
                }
                storeIds = [storeIds stringByAppendingString:stroeId];
                GoodsInfos = [GoodsInfos stringByAppendingString:goodsInfos];
            }
            [dic setObject:storeIds forKey:@"storeId"];
            [dic setObject:GoodsInfos forKey:@"goodsInfo"];
            [dic setObject:_addressDic[@"msadId"] forKey:@"addressId"];
            //Juge 0 不用积分 1全部积分 2部分积分
            if (_scroButton.ifSelected==YES) {
                [dic setObject:@"" forKey:@"integral"];
                if (_ifCodePayIndex==53) {
                    [dic setObject:@"0" forKey:@"Juge"];
                }else if (_ifCodePayIndex==54){
                    [dic setObject:@"1" forKey:@"Juge"];
                }else{
                    [dic setObject:@"2" forKey:@"Juge"];
                    [dic setObject:_cutDownCode.text forKey:@"integral"];
                    
                }
                
            }else{
                [dic setObject:@"0" forKey:@"Juge"];
                [dic setObject:@"" forKey:@"integral"];
            }
            
            if (!_forSaleIds) {//优惠券
                [dic setObject:@"-" forKey:@"mscoId"];
            }else{
               [dic setObject:_forSaleIds forKey:@"mscoId"];
            }
            
            if (_ifNeedPaperIndex==50) {//不需要发票
                [dic setObject:@"1" forKey:@"moexBillType"];
            }else if (_ifNeedPaperIndex==51){//个人
                [dic setObject:@"2" forKey:@"moexBillType"];
            }else if (_ifNeedPaperIndex==52){
                [dic setObject:@"3" forKey:@"moexBillType"];
                [dic setObject:_othersField.text forKey:@"moexBillName"];
            }
            
            self.shareApi.delegate=self;
            [self.shareApi  requestOperationWithDic:dic withPath:SX_ShoppingCarIndent withHttpType:@"POST" withTag:SX_SX_CarCreateOrderTag];
        }else{//商品详情立即购买
            NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
            
            [dic setObject:_addressDic[@"msadId"] forKey:@"addressId"];
            [dic setObject:_goodsInfoArray[0][@"mcdeUpspecifyId"] forKey:@"mcdeUpspecifyId"];
            [dic setObject:_goodsInfoArray[0][@"goodsCartNum"] forKey:@"count"];
            [dic setObject:_goodsInfoArray[0][@"mgooId"] forKey:@"goodsId"];
            [dic setObject:_goodsInfoArray[0][@"mallStoreId"] forKey:@"storeId"];
            if (_scroButton.ifSelected==YES) {
                [dic setObject:@"" forKey:@"integral"];
                if (_ifCodePayIndex==53) {
                    [dic setObject:@"0" forKey:@"Juge"];
                }else if (_ifCodePayIndex==54){
                    [dic setObject:@"1" forKey:@"Juge"];
                }else{
                    [dic setObject:@"2" forKey:@"Juge"];
                    [dic setObject:_cutDownCode.text forKey:@"integral"];
                    
                }
                
            }else{
                [dic setObject:@"0" forKey:@"Juge"];
            }
            
            if (!_forSaleIds) {//优惠券id
                [dic setObject:@"-" forKey:@"mscoId"];
            }else{
                [dic setObject:_forSaleIds forKey:@"mscoId"];
            }
            
            if (_ifNeedPaperIndex==50) {//不需要
                [dic setObject:@"1" forKey:@"moexBillType"];
            }else if (_ifNeedPaperIndex==51){//个人
            [dic setObject:@"2" forKey:@"moexBillType"];
            }else if (_ifNeedPaperIndex==52){
            [dic setObject:@"3" forKey:@"moexBillType"];
            [dic setObject:_othersField.text forKey:@"moexBillName"];
            }
            
            self.shareApi.delegate=self;
            [self.shareApi  requestOperationWithDic:dic withPath:SX_appSavaOrder withHttpType:@"POST" withTag:SX_SX_createPackFruitTag];
        }
        
    }
    
    
}
- (IBAction)closeView:(id)sender {
    _maskView.hidden = YES;
    _ifNeedPaperView.hidden = YES;
    _codePayView.hidden = YES;
    _forSaleView.hidden = YES;
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

#pragma mark == 查询用户所有积分
- (void)getPersonalCode{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryMypoits withHttpType:@"POST" withTag:SX_SX_queryMypoits];
    
    
}
#pragma mark == 查询积分抵扣额度

- (void)getCodeRule{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_querySpendingSys withHttpType:@"POST" withTag:SX_SX_querySpendingSys];
    
    
}

#pragma mark == 查询用户是否拥有店铺优惠劵
- (void)ifHaveForSalePaper{

    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
        NSString * storeIds = @"";
        for (int i = 0; i<_goodsInfoArray.count; i++) {
            NSString * storeId = [NSString stringWithFormat:@",%@",_goodsInfoArray[i][@"mallStoreId"]];
            if (i==0) {
                storeId = [NSString stringWithFormat:@"%@",_goodsInfoArray[i][@"mallStoreId"]];
            }
            storeIds = [storeIds stringByAppendingString:storeId];
        }
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"UserId"];
    [dic setObject:storeIds forKey:@"storeId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryDesc withHttpType:@"POST" withTag:SX_SX_queryDesc];
    
}

#pragma mark == 发票
- (IBAction)openIfNeedView:(id)sender {
    _maskView.hidden = NO;
    _ifNeedPaperView.hidden = NO;
}

#pragma mark == 优惠劵
- (IBAction)forSalePaper:(id)sender {
     _maskView.hidden = NO;
    _forSaleView.hidden = NO;
}

#pragma mark == 选择是否需要发票
- (IBAction)ifNeedPaperHandel:(UIButton *)sender {
    sender.selected = YES;
    if (sender.tag == _ifNeedPaperIndex) {
        return;
    }
    UIButton * button = [self.view viewWithTag:_ifNeedPaperIndex];
    button.selected = NO;
    _ifNeedPaperIndex = sender.tag;
    
}
#pragma mark == 确定是否需要发票
- (IBAction)sureIfNeedPaper:(id)sender {
    _maskView.hidden = YES;
    _ifNeedPaperView.hidden = YES;
    NSArray * word = @[@"不开发票",@"个人",@"单位"];
    _ifNeedPaperLabel.text = word[_ifNeedPaperIndex-50];
    [self hideKeyBoard];
}


#pragma mark == 选择是否抵扣积分
- (IBAction)ifCodePay:(UIButton *)sender {
    sender.selected = YES;
    if (sender.tag == _ifCodePayIndex) {
        return;
    }
    UIButton * button = [self.view viewWithTag:_ifCodePayIndex];
    button.selected = NO;
    _ifCodePayIndex = sender.tag;
     [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

#pragma mark == 确定是否抵扣积分
- (IBAction)sureIfCodePay:(id)sender {
    _maskView.hidden = YES;
    _codePayView.hidden = YES;

}

#pragma mark  == 进入商店
- (void)goToShop:(UIButton *)sender{
    //    _goodsArray[sender.tag/100-1]
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    ShopViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Shop"];
    page.storeId =  _goodsInfoArray[sender.tag/100-1][@"mallStoreId"];
    [self.navigationController   pushViewController:page animated:YES];
}
#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == _tableView) {
        return _goodsInfoArray.count;
    }else{
        return 1;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        return 102*VerticalRatio();
    }else{
    return 120*VerticalRatio();
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == _tableView) {
        return 38*VerticalRatio();
    }else{
        return 0;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        NSArray * array = _goodsInfoArray[section][@"goodsList"];
        if (array.count > 0) {
            return array.count;
        }else{
            return 1;
        }
    }else{
        return _forSalePaperArray.count;
    }
   
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == _tableView) {
        
    UIView * view = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 38), NO)];
    view.backgroundColor = [UIColor whiteColor];
  
    UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 37, 320, 1), YES)];
    line.backgroundColor = [UIColor colorWithWhite:0.945 alpha:1.000];
    [view addSubview:line];
    
    UIImageView * home = [[UIImageView alloc]initWithFrame:flexibleFrame(CGRectMake(10, 11, 19, 17), YES)];
    home.image = [UIImage imageNamed:@"shopping15"];
    [view addSubview:home];
        
    UILabel * label =  [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(35,0, 265, 38), NO)];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor colorWithWhite:0.196 alpha:1.000];
    label.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];
    label.text = _goodsInfoArray[section][@"mallStoreName"];
    [view addSubview:label];
    
    UIImageView * image = [[UIImageView alloc]initWithFrame:flexibleFrame(CGRectMake(300, 12, 8, 14), YES)];
    image.image = [UIImage imageNamed:@"film1"];
    [view addSubview:image];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = flexibleFrame(CGRectMake(0, 0, 320, 38), YES);
    button.backgroundColor = [UIColor clearColor];
    button.tag = 100*(section+1);
    [button addTarget:self action:@selector(goToShop:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
        return view;
    }
    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView!=_tableView) {
        MyForSaleCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifer];
        if (cell == nil) {
            cell= [[MyForSaleCardTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifer];
            
        }
        
        for (UIView *view in cell.subviews) {
            if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
                [view removeFromSuperview];
            }
        }
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        NSDictionary * dic= _forSalePaperArray[indexPath.row];
        cell.forSaleMoney.text = [NSString stringWithFormat:@"¥%@代金劵",dic[@"mscoDecreaseAmount"]];
        cell.storeName.text =[NSString stringWithFormat:@"%@", dic[@"storeName"]];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        NSDate *confromTimesp1 = [NSDate dateWithTimeIntervalSince1970:[dic[@"mscoInvalidateDate"] integerValue]/1000];
        [formatter setDateFormat:@"yyyy.MM.dd"];
        NSString *endTime = [formatter stringFromDate:confromTimesp1];
        cell.timeLabel.text = [NSString stringWithFormat:@"有效期至%@",endTime];
        cell.timeOutButton.hidden = YES;
        return cell;

    }else{
    SureIndentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];
    if (cell == nil) {
        cell= [[SureIndentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifer];
        
    }
        
        for (UIView *view in cell.subviews) {
            if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
                [view removeFromSuperview];
            }
        }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    NSArray * array = _goodsInfoArray[indexPath.section][@"goodsList"];
    if (array.count > 0) {
        NSString * image = _goodsInfoArray[indexPath.section][@"goodsList"][indexPath.row][@"appAdertImagUriName"];
        if (image.length>0) {
            [cell.indentImage sd_setImageWithURL:[NSURL URLWithString:image]];
        }else{
            cell.indentImage.image =[UIImage imageNamed:@"cloth1"];
        }
        cell.indentName.text = [NSString stringWithFormat:@"%@",_goodsInfoArray[indexPath.section][@"goodsList"][indexPath.row][@"mgooName"]];
        cell.indentName.frame = flexibleFrame(CGRectMake(104, 16, 204, 20), NO);
        cell.indentName.numberOfLines = 0;
        [cell.indentName sizeToFit];
        
        cell.indentInfo.text = [NSString stringWithFormat:@"%@",_goodsInfoArray[indexPath.section][@"goodsList"][indexPath.row][@"mgseSpecifyDetailJson"]];
        cell.indentInfo.frame = flexibleFrame(CGRectMake(104, 47, 204, 20), NO);
        cell.indentInfo.numberOfLines = 0;
        [cell.indentInfo sizeToFit];
        cell.indentNumber.text = [NSString stringWithFormat:@"x%@",_goodsInfoArray[indexPath.section][@"goodsList"][indexPath.row][@"goodsCartNum"]];
        cell.indentPrice.text = [NSString stringWithFormat:@"￥%@",_goodsInfoArray[indexPath.section][@"goodsList"][indexPath.row][@"mgooPublishPrice"]];
    }else{
        NSString *appAdertImagUriName = _goodsInfoArray[0][@"appAdertImagUriName"];
        NSArray *urlArr = [appAdertImagUriName componentsSeparatedByString:@","];
        NSString * image = urlArr[0];
        if (image.length>0) {
            [cell.indentImage sd_setImageWithURL:[NSURL URLWithString:image]];
        }else{
            cell.indentImage.image =[UIImage imageNamed:@"cloth1"];
        }
        cell.indentName.numberOfLines = 0;
        cell.indentName.frame = flexibleFrame(CGRectMake(104, 16, 204, 0), NO);
        cell.indentName.text = [NSString stringWithFormat:@"%@",_goodsInfoArray[0][@"mgooName"]];
        [cell.indentName sizeToFit];
        
        cell.indentInfo.numberOfLines = 0;
        cell.indentInfo.frame = flexibleFrame(CGRectMake(104, 50, 204, 0), NO);
        cell.indentInfo.text = [NSString stringWithFormat:@"%@",_goodsInfoArray[0][@"mgseSpecifyDetailJson"]];
        [cell.indentInfo sizeToFit];
        
        cell.indentNumber.text = [NSString stringWithFormat:@"x%@",_goodsInfoArray[0][@"goodsCartNum"]];
        cell.indentPrice.text = [NSString stringWithFormat:@"￥%@",_goodsInfoArray[0][@"mgooPublishPrice"]];
    }
     return cell;
    }

    return nil;
   
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _forSaleTableView) {
        _forSaleIds = _forSalePaperArray[indexPath.row][@"mscoId"];
        _ifUseForSalePaper.text = @"已使用";
        _maskView.hidden = YES;
        _forSaleView.hidden = YES;

    }
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    
    if (tag==SX_SX_CarCreateOrderTag) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:2];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            PayViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
//            page.payInfoArray = _goodsInfoArray;
            page.payOrderName = @"凡人街在线商城";
            page.payOrderPrice = self.countMoney;
            NSArray *arr = database[@"result"];
            NSString *orderId = @"";
            for (NSDictionary *dic in arr) {
                orderId = dic[@"mgorNo"];
            }
            page.payId = orderId;

            [self.navigationController   pushViewController:page animated:YES];
            
        }
        
    }else if (tag==SX_SX_GetDefaultAddress) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic = (NSDictionary *)database[@"result"];
            if(![dic isEqual:[NSNull null]]){
                if (dic.allValues.count>=3) {
                    _addressView.hidden = NO;
                    _addressName.text = [NSString stringWithFormat:@"%@  %@",dic[@"msadReceiverName"],dic[@"msadMobileNo"]] ;
                    _addressAddress.text = dic[@"msadAddr"];
                    _addressDic = dic;
                }else{
                    _addressView.hidden = YES;
                }
            }
            
        }
        
    }else if (tag == SX_SX_createPackFruitTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:2];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            PayViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
            page.payOrderName = @"凡人街在线商城";
            page.payOrderPrice = self.countMoney;
            page.type = 1;
            page.payId = database[@"result"][@"mgorNo"];
            [self.navigationController   pushViewController:page animated:YES];
            
        }

    }else if (tag == SX_SX_queryDesc){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            
            _forSalePaperArray = database[@"result"];
            _forSaleTableView.dataSource = self;
            _forSaleTableView.delegate = self;
            [_forSaleTableView reloadData];
            _forSalePaperNumber.text = [NSString stringWithFormat:@"%ld张可用",_forSalePaperArray.count];
        }
    
    }else if (tag == SX_SX_queryMypoits){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _sumCode.text = [NSString stringWithFormat:@"%@",database[@"result"][@"myPoints"]];
            [self getCodeRule];
        }
    }else if (tag == SX_SX_querySpendingSys){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic = database[@"result"];
            _codeRule.text = [NSString stringWithFormat:@"%@积分抵扣%@元",dic[@"Spenging"],dic[@"Money"]];
            _codeUseRule.text = [NSString stringWithFormat:@"%@积分抵扣%@元",dic[@"Spenging"],dic[@"Money"]];
            _cutDownMoney.text = [NSString stringWithFormat:@"-¥%.2f",[_sumCode.text floatValue]/[dic[@"Spenging"]floatValue]];

        }
    }
    
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
